package cn.tansanqinger.service.group.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tansanqinger.dao.sql.IDisposeresultSqlDao;
import cn.tansanqinger.dao.sql.account.UserAccountSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.ChatMessageSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.GroupNumberSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.GroupSqlDaoImpl;
import cn.tansanqinger.dao.sql.group.ReportUserAndGroupSqlDaoImpl;
import cn.tansanqinger.pojo.Chatmessage;
import cn.tansanqinger.pojo.Disposeresult;
import cn.tansanqinger.pojo.Flick;
import cn.tansanqinger.pojo.Groupmember;
import cn.tansanqinger.pojo.Reportuser;
import cn.tansanqinger.pojo.User;
import cn.tansanqinger.service.group.IGroupAndNumberService;
import cn.tansanqinger.util.DateUtil;
import cn.tansanqinger.util.Tools;

@Service
public class GroupAndNumberServiceImpl implements IGroupAndNumberService{
	
	@Autowired
	private GroupSqlDaoImpl gimpl;
	@Autowired
	private GroupNumberSqlDaoImpl mimpl;
	@Autowired
	private UserAccountSqlDaoImpl uimpl;
	@Autowired
	private ReportUserAndGroupSqlDaoImpl rimpl;
	@Autowired
	private ChatMessageSqlDaoImpl cimpl;
	@Autowired
	private IDisposeresultSqlDao ddao;
	

	@Override
	public Map<String, Object> selectGroup(HttpServletRequest request, int isReport) {
		String keywords = Tools.isEmpty(request.getParameter("keywords"));
		String page = Tools.isEmpty(request.getParameter("page"));
		String limit = Tools.isEmpty(request.getParameter("limit"));
		String isbanned = Tools.isEmpty(request.getParameter("isbanned"));
		String time = Tools.isEmpty(request.getParameter("time"));
		String reportTime = Tools.isEmpty(request.getParameter("reportTime"));
		int pageInt = -1;
		if(page.matches("\\d+")) {
			pageInt = Integer.parseInt(page);
		}
		int limitInt = -1;
		if(limit.matches("\\d+")) {
			limitInt = Integer.parseInt(limit);
		}
		int isbannedInt = -1;
		if("0".equals(isbanned)||"1".equals(isbanned)) {
			isbannedInt = Integer.parseInt(isbanned);
		}
		long startTime = -1;
		long endTime = -1;
		if(!"".equals(time)) {
			startTime = DateUtil.getTime2(time.substring(0,10))/1000;
			endTime = DateUtil.getTime2(time.substring(13,23))/1000;
		}
		long reportStartTime = -1;
		long reportEndTime = -1;
		if(!"".equals(reportTime)) {
			reportStartTime = DateUtil.getTime2(reportTime.substring(0,10))/1000;
			reportEndTime = DateUtil.getTime2(reportTime.substring(13,23))/1000;
		}
		return gimpl.selectMessage(keywords, isbannedInt, startTime, endTime, reportStartTime, reportEndTime, pageInt, limitInt, isReport);
	}

	@Override
	public Map<String, Object> bannedGroup(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			Flick flick = gimpl.selectOnt(Integer.parseInt(id));
			if(flick!=null) {
				//需要查询举报的用户，然后进行处理
				List<Reportuser> reportusers = rimpl.selectReport(flick.getGid(), 1);
				for (int i = 0; i < reportusers.size(); i++) {
					reportusers.get(i).setRisdispose(1);
					reportusers.get(i).setRdisposemessage("管理员封禁了该群聊");
					rimpl.modification(reportusers.get(i));
					Disposeresult disposeresult = new Disposeresult();
					disposeresult.setOid(reportusers.get(i).getRid());
					disposeresult.setUid(reportusers.get(i).getUid());
					disposeresult.setDmessage("群聊《"+flick.getGname()+"》被管理员封禁了");
					disposeresult.setIsselect(0);
					disposeresult.setDtype(3);
					disposeresult.setDtime(System.currentTimeMillis()/1000);
					disposeresult.setDdate(DateUtil.getTime());
					ddao.insert(disposeresult);
					disposeresult = new Disposeresult();
					disposeresult.setOid(reportusers.get(0).getRid());
					disposeresult.setUid(flick.getGuid());
					disposeresult.setDmessage("你的群聊《"+flick.getGname()+"》被管理员封禁了");
					disposeresult.setIsselect(0);
					disposeresult.setDtype(3);
					disposeresult.setDtime(System.currentTimeMillis()/1000);
					disposeresult.setDdate(DateUtil.getTime());
					ddao.insert(disposeresult);
				}
				flick.setIsreport(0);
				flick.setIsbanned(1);
				if(gimpl.modification(flick)==1) {
					resultMap.put("code","0");
					resultMap.put("msg", "封禁成功");
				} else {
					resultMap.put("code","-1");
					resultMap.put("msg", "封禁失败");
				}
				
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据异常");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> ignoreGroup(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			Flick flick = gimpl.selectOnt(Integer.parseInt(id));
			if(flick!=null) {
				//需要查询举报的用户，然后进行处理
				List<Reportuser> reportusers = rimpl.selectReport(flick.getGid(), 1);
				for (int i = 0; i < reportusers.size(); i++) {
					reportusers.get(i).setRisdispose(1);
					reportusers.get(i).setRdisposemessage("管理员已经对群聊做出了警告！");
					rimpl.modification(reportusers.get(i));
					Disposeresult disposeresult = new Disposeresult();
					disposeresult.setOid(reportusers.get(i).getRid());
					disposeresult.setUid(reportusers.get(i).getUid());
					disposeresult.setDmessage("管理员对群聊《"+flick.getGname()+"》做出了警告");
					disposeresult.setIsselect(0);
					disposeresult.setDtype(3);
					disposeresult.setDtime(System.currentTimeMillis()/1000);
					disposeresult.setDdate(DateUtil.getTime());
					ddao.insert(disposeresult);
				}
				Disposeresult disposeresult = new Disposeresult();
				disposeresult.setOid(reportusers.get(0).getRid());
				disposeresult.setUid(flick.getGuid());
				disposeresult.setDmessage("你创建的群聊《"+flick.getGname()+"》被举报，请你管理好群成员，注意言辞，再次发现将封禁群聊");
				disposeresult.setIsselect(0);
				disposeresult.setDtype(3);
				disposeresult.setDtime(System.currentTimeMillis()/1000);
				disposeresult.setDdate(DateUtil.getTime());
				ddao.insert(disposeresult);
				flick.setIsreport(0);
				flick.setIsbanned(0);
				if(gimpl.modification(flick)==1) {
					resultMap.put("code","0");
					resultMap.put("msg", "处理成功");
				} else {
					resultMap.put("code","-1");
					resultMap.put("msg", "处理失败");
				}
				
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据异常");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteGroup(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		if(id.matches("\\d+")) {
			if(gimpl.deleteOne(Integer.parseInt(id))==1) {
				resultMap.put("code","0");
				resultMap.put("msg", "删除成功");
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "删除失败");
			}
				
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public List<User> selectGroupNumber(HttpServletRequest request) {
		List<Groupmember> groupmembers = mimpl.selectGroupAll(Tools.isEmpty(request.getParameter("account")));
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < groupmembers.size(); i++) {
			User user = uimpl.selectId(groupmembers.get(i).getUid());
			if(user!=null) {
				if(!"".equals(groupmembers.get(i).getUname())) {
					user.setName(groupmembers.get(i).getUname());
					if(groupmembers.get(i).getJurisdiction()==0) {
						user.setPersignature("群主（教师）");
					} else {
						if(user.getJurisdiction()==0) {
							user.setPersignature("学生");
						} else {
							user.setPersignature("教师");
						}
					}
					user.setCreatetime(groupmembers.get(i).getMdata());
				}
				list.add(user);
			}
		}
		return list;
	}

	@Override
	public Map<String, Object> bannedGroupNumber(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String id = Tools.isEmpty(request.getParameter("id"));
		String gid = Tools.isEmpty(request.getParameter("gid"));
		
		if(id.matches("\\d+")) {
			Groupmember groupmember = mimpl.selectGroup(Integer.parseInt(id));
			if(groupmember!=null) {
				groupmember.setIsbanned(1);
				if(mimpl.modification(groupmember)==1) {
					resultMap.put("code","0");
					resultMap.put("msg", "封禁成功");
				} else {
					resultMap.put("code","-1");
					resultMap.put("msg", "封禁失败");
				}
				if(gid.matches("\\d+")) {
					Flick flick = gimpl.selectOnt(Integer.parseInt(gid));
					if(flick!=null) {
						//需要查询举报的用户，然后进行处理
						List<Reportuser> reportusers = rimpl.selectReport(flick.getGid(), 1);
						for (int i = 0; i < reportusers.size(); i++) {
							reportusers.get(i).setRisdispose(1);
							reportusers.get(i).setRdisposemessage("管理员封禁了账号为："+groupmember.getUaccount()+"的用户！");
							rimpl.modification(reportusers.get(i));
						}
						flick.setIsreport(0);
						gimpl.modification(flick);
					} else {
						resultMap.put("code","-1");
						resultMap.put("msg", "数据异常");
					}
				}
			} else {
				resultMap.put("code","-1");
				resultMap.put("msg", "数据异常");
			}
		} else {
			resultMap.put("code","-1");
			resultMap.put("msg", "数据异常");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectChatMessage(HttpServletRequest request) {
		String id = Tools.isEmpty(request.getParameter("gid"));
		String account = Tools.isEmpty(request.getParameter("account"));
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> users = selectGroupNumber(request);
		List<Reportuser> reportusers = rimpl.selectReport(Integer.parseInt(id), 1);
		for (int i = 0; i < reportusers.size(); i++) {
			for (int j = 0; j < users.size(); j++) {
				if(reportusers.get(i).getUid()==users.get(j).getId()) {
					if(!"".equals(users.get(j).getName())) {
						reportusers.get(i).setUname(users.get(j).getName());
					} else {
						reportusers.get(i).setUname(users.get(j).getAccount());
					}
				}
			}
		}
		List<Chatmessage> chatmessages = cimpl.selectChatMessageAll(account);
		for (int i = 0; i < chatmessages.size(); i++) {
			for (int j = 0; j < users.size(); j++) {
				if(chatmessages.get(i).getFaccount().equals(users.get(j).getAccount())) {
					if(!"".equals(users.get(j).getName())) {
						chatmessages.get(i).setUname(users.get(j).getName());
					} else {
						chatmessages.get(i).setUname(users.get(j).getAccount());
					}
				}
			}
		}
		map.put("reportusers", reportusers);
		map.put("chatmessage", chatmessages);
		
		return map;
	}

}
